package com.rainbow.security

import com.rainbow.security.service.UserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Configurable
import org.springframework.security.config.annotation.ObjectPostProcessor
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.builders.WebSecurity
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor

@Configurable
@EnableGlobalMethodSecurity(prePostEnabled = true)
class WebSecurityConfig:WebSecurityConfigurerAdapter() {


    @Autowired
    private lateinit var userService:UserService

    @Autowired
    internal var urlAccessDecisionManager: UrlAccessDecisionManager? = null
    @Autowired
    internal var deniedHandler: AuthenticationAccessDeniedHandler? = null



    override fun configure(auth: AuthenticationManagerBuilder) {
        auth.userDetailsService(userService)
                .passwordEncoder(BCryptPasswordEncoder())

    }

    override fun configure(web: WebSecurity) {
        web.ignoring().antMatchers("/druid/**","/static/**")
    }

    override fun configure(http: HttpSecurity) {
        http.authorizeRequests()
                .withObjectPostProcessor(object : ObjectPostProcessor<FilterSecurityInterceptor>() {
                    @Override
                    fun <O : FilterSecurityInterceptor> postProcess(o: O): O {
                        o.setSecurityMetadataSource(metadataSource)
                        o.setAccessDecisionManager(urlAccessDecisionManager)
                        return o
                    }
                })
    }
}